@extends('admin.base')


@section('content')
  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h1>
      <i class="fa fa-calendar"></i>
      商品添加页面
    </h1>
    <ol class="breadcrumb">
      <li><a href="#"><i class="fa fa-dashboard"></i> 首页</a></li>
      <li><a href="#">商品信息管理</a></li>
      <li class="active">添加商品信息</li>
    </ol>
  </section>

  <!-- Main content -->
  <section class="content">
    <div class="row">
      <!-- right column -->
      <div class="col-md-12">
        <!-- Horizontal Form -->
        <div class="box box-primary">
          <div class="box-header with-border">
            <h3 class="box-title"><i class="fa fa-plus"></i> 添加商品信息</h3>
          </div><!-- /.box-header -->
          <!-- form start -->
          <form class="form-horizontal" action="{{URL('admin/goods')}}" method="post" id="goods_form" enctype="multipart/form-data">
            {{csrf_field()}}
            <div class="box-body">
              <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">父类名称</label>
                <div class="col-sm-4">
                  <select  name="cate_id" class="form-control" value="{{old('cate_name')}}">
                    @foreach($list as $v)
                      <option  value="{{$v->cate_id}}">{{$v->cate_name}}</option>
                  @endforeach
                  </select>
                </div>
              </div>
              <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label" >商品名称</label>
                <div class="col-sm-4">
                  <input type="text" name="goods_title" value="{{old('goods_title')}}" class="form-control">
                </div>
              </div>
              <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">商品图片</label>
                <div class="col-sm-4">
                  <input type="text" name="goods_img" id="goods_img" class="form-control" value="{{old('goods_img')}}">
                  <input type="file" name="file_upload" id="file_upload" value="" class="form-control">
                  <p><img src="" alt="" id="img1" style="width:100px" hidden></p>
                  <script type="text/javascript">
                      $(function () {
                          $("#file_upload").change(function () {
                              $('img1').show();
                              uploadImage();
                          });
                      });
                      function uploadImage() {
//                            判断是否有选择上传文件
                          var imgPath = $("#file_upload").val();
                          if (imgPath == "") {
                              alert("请选择上传图片！");
                              return;
                          }
                          //判断上传文件的后缀名
                          var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
                          if (strExtension != 'jpg' && strExtension != 'gif'
                              && strExtension != 'png' && strExtension != 'bmp') {
                              alert("请选择图片文件");
                              return;
                          }
                          var formData = new FormData($('#goods_form')[0]);
                          $.ajax({
                              type: "POST",
                              url: "/admin/upload",
                              data: formData,
                              async: true,
                              cache: false,
                              contentType: false,
                              processData: false,
                              success: function(data) {
                                  $('#img1').attr('src','/'+data);
                                  $('#img1').show();
                                  $('#goods_img').val(data);


                              },
                              error: function(XMLHttpRequest, textStatus, errorThrown) {
                                  alert("上传失败，请检查网络后重试");
                              }
                          });
                      }

                  </script>
                </div>
              </div>
              <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">商品信息</label>
                <div class="col-sm-4">
                  <input type="text" name="goods_info" class="form-control" value="{{old('goods_info')}}">
                </div>
              </div>
              <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">商品状态</label>
                <div class="col-sm-4">
                  <select  name="goods_status" class="form-control" value="{{old('cate_status')}}">
                      <option selected value="1">新添加</option>
                      <option  value="2">在售</option>
                      <option  value="3">已下架</option>
                  </select>
                </div>
              </div>
            </div><!-- /.box-body -->
            <div class="box-footer">
              <div class="col-sm-offset-2 col-sm-1">
                <button type="submit" class="btn btn-primary">添加</button>
              </div>
              <div class="col-sm-1">
                <button type="submit" class="btn btn-primary">重置</button>
              </div>
            </div><!-- /.box-footer -->
          </form>
          <div class="row"><div class="col-sm-12">&nbsp;</div></div>
          <div class="row"><div class="col-sm-12">
              <br/>
              @if (count($errors) > 0)
                <div class="alert alert-danger">
                  <ul>
                    @foreach ($errors->all() as $error)
                      <li>{{ $error }}</li>
                    @endforeach
                  </ul>
                </div>
              @endif
            </div></div>
        </div><!-- /.box -->

      </div><!--/.col (right) -->
    </div>   <!-- /.row -->
  </section><!-- /.content -->

@endsection